package com.junzhiit.nengji.cms.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.junzhiit.nengji.bo.VipBO;
import com.junzhiit.nengji.cms.service.VipService;
import com.junzhiit.nengji.cms.web.exception.VipAlreadyExistException;
import com.junzhiit.nengji.cms.web.util.ViewObjectUtil;
import com.junzhiit.nengji.cms.web.vo.DataTablesParamVO;
import com.junzhiit.nengji.cms.web.vo.ResultVO;
import com.junzhiit.nengji.cms.web.vo.VipVO;
import com.junzhiit.nengji.dao.VipDAO;
import com.junzhiit.nengji.model.Vip;
import com.junzhiit.nengji.model.enums.IdentityType;

@Service
public class VipServiceImpl extends BaseServiceImpl implements VipService {

	@Autowired
	private VipDAO vipDAO;

	@Override
	public Map<String, Object> findAll(DataTablesParamVO dtp) {
		List<Vip> list = vipDAO.findAll(dtp.getStart(), dtp.getLength());
		int totalCount = vipDAO.getTotalCount();
		return ViewObjectUtil.dataTables(list, dtp.getDraw(), totalCount);
	}

	@Override
	public ResultVO add(VipVO vipVO) {
		ResultVO result = new ResultVO();
		if(vipDAO.getVipByIdentity(vipVO.getIdentity()) != null){
			throw new VipAlreadyExistException("该用户身份已设置，请重新选择.");
		}
		Vip vip = new Vip();
		BeanUtils.copyProperties(vipVO, vip);
		vipDAO.insert(vip);
		return result;
	}

	@Override
	public ResultVO del(String id) {
		ResultVO result = new ResultVO();
		vipDAO.updateToDelete(id);
		return result;
	}

	@Override
	public ResultVO findById(String id) {
		ResultVO result = new ResultVO();
		Vip vip=vipDAO.query(id);
		result.setData(vip);
		return result;
	}

	@Override
	public ResultVO update(VipVO vipVO) {
		ResultVO result = new ResultVO();
		Vip vip=vipDAO.query(vipVO.getId());
		BeanUtils.copyProperties(vipVO, vip);
		vipDAO.update(vip);
		return result;
	}

	@Override
	public ResultVO getVipName() {
		ResultVO result = new ResultVO();
		List<VipBO> vips = vipDAO.getVipName();
		for(VipBO vip : vips){
			if(vip.getIdentity() != null){
				if(vip.getIdentity() == IdentityType.COMMON.toIntType()){
					vip.setVipName(IdentityType.COMMON.toStringValue());
				}else if(vip.getIdentity() == IdentityType.VIP_USER.toIntType()){
					vip.setVipName(IdentityType.VIP_USER.toStringValue());
				}else if(vip.getIdentity() == IdentityType.CHANNEL_PARTNER.toIntType()){
					vip.setVipName(IdentityType.CHANNEL_PARTNER.toStringValue());
				}
			}
		}
		result.setData(vips);
		return result;
	}

}
